<?php
namespace app\admin\controller;



use app\admin\logic\GoodsLogic;
use app\admin\model\Brand;
use app\admin\model\Gcategory;
use app\admin\model\GoodsProp;
use app\admin\model\GoodsPropValue;
use app\BaseController;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Goods extends BaseController {
    //商品页
    public function goodList(){
       //where条件封装
        $where=GoodsLogic::setWhere();
        //留值设置
        $data=GoodsLogic::setData();
        View::assign($data);
        //查询排序
        $getorder = GoodsLogic::getOrder();
//        View::assign($getorder);
        //查询数据
        $list=\app\admin\model\Goods::getGoodsList($where,$getorder);
        View::assign("list",$list);
        //查询分类
        $cate=\app\admin\model\Gcategory::goodsCateList();
        View::assign("cate",$cate);
        //查询品牌
        $pin=\app\admin\model\Brand::goodsPinList();
        View::assign('pin',$pin);
        return View::fetch();

    }




//todo==========================商品添加====================================
    public function goodAdd(){
        $param=Request::param();


        //查询分类
        $cate=\app\admin\model\Gcategory::goodsCateList();
        View::assign("cate",$cate);
        //查询品牌
        $pin=\app\admin\model\Brand::goodsPinList();
        View::assign('pin',$pin);

      if (Request::isPost()){
          $data=[
              'goods_name'=>$param['name'],
              'price'=>$param['price'],
              'vip'=>$param['price_shop'],
              'spec_qty'=>$param['num'],
              'cate_id'=>$param['cid'],
              'brand'=>$param['brand'],
              'recommended'=>$param['recommended'],
              'is_show'=>$param['is_show'],
              'if_show'=>$param['if_show'],
              'description'=>$param['content'],

          ];
//          echo "<pre>";
//          print_r($data);
//          echo "</pre>";
//          die;
          list($res,$msg)=\app\admin\model\Goods::addGoodsList($data);
          if ($res){
              //成功
              $this->success($msg,url('goodList'),[],3);
          }else{
              //失败
              $this->error($msg,url('goodAdd'),[],3);
          }
      }else{
          return View::fetch();
      }

    }
    //todo================================商品修改===========================
    public function goodEdit(){

        //查询分类
        $cate=\app\admin\model\Gcategory::goodsCateList();
        View::assign("cate",$cate);
        //查询品牌
        $pin=\app\admin\model\Brand::goodsPinList();
        View::assign('pin',$pin);
        //=====================================================================

       if (Request::isGet()){
           $param=Request::param();

           //$id=Request::get('id');
           $num = \app\admin\model\Goods::find($param['goods_id'])->toArray();
           View::assign('num',$num);



       }else{
           $param=Request::param();
           $data=[
               'goods_id'=>$param['goodsid'],
               'goods_name'=>$param['name'],
               'price'=>$param['price'],
               'vip'=>$param['price_shop'],
               'spec_qty'=>$param['num'],
               'cate_id'=>$param['cid'],
               'brand'=>$param['brand'],
               'recommended'=>$param['recommended'],
               'is_show'=>$param['is_show'],
               'if_show'=>$param['if_show'],
               'description'=>$param['content'],
           ];

           list($res,$msg)=\app\admin\model\Goods::editGoodsList($data);
           if ($res){
               //成功
               if (isset($param['come']) && !empty($param['come'])){
                   $this->success($msg,url('goodEdit',['goods_id'=>$param['goodsid']]));
               }
               if (isset($param['back']) && !empty($param['back'])){
                   $this->success($msg,url('goodList'));
               }
           }else{
               //失败
               $this->error($msg,url('goodEdit'),[],3);
           }

       }

        return View::fetch();
    }
//todo=================================分类页=====================================
    //分类页
    public function cate(){
        //处理参数
        $paramNew=GoodsLogic::newData();
        //查询分类数据
        $list=Gcategory::getCateList($paramNew);
        //增加next_cate字段  用于判断是否可以点击下一分类
        $listNew=GoodsLogic::getNextCate($list);
        View::assign("list",$listNew);
        //查询分类
        $cate=\app\admin\model\Gcategory::goodsCateList();
        View::assign("cate",$cate);
        //todo================================添加===========================
        if (Request::isPost()){
            $param=Request::param();
            $data=[
                'parent_id'=>$param['parentid'],
                'cate_name'=>$param['name'],
                'sort_order'=>$param['sort'],

            ];

            list($res,$msg)=\app\admin\model\Gcategory::cateAdd($data);
            if ($res){
                $this->success($msg,url('cate'),[],3);
            }else{
                $this->error($msg,url('cate'),[],3);
            }

        }else{
            return View::fetch();
        }

    }
    //todo==============================分类修改=======================================
    public function cateedit(){
        //查询分类
        $cate=\app\admin\model\Gcategory::goodsCateList();
        View::assign("cate",$cate);
       if (Request::isGet()){
           $param=Request::param();
           $cateone=Gcategory::find($param['cate_id'])->toArray();
//           echo "<pre>";
//           print_r($cates);
//           echo "</pre>";
        View::assign('cateone',$cateone);
       }else{
           $param=Request::param();
           $data=[
              'cate_id'=>$param['cateid'],
               'cate_name'=>$param['name'],
               'parent_id'=>$param['parentid'],
               'sort_order'=>$param['sort'],
           ];
           list($res,$msg)=Gcategory::editCateList($data);
           if ($res){
               if (isset($param['come']) && !empty($param['come'])){
                   $this->success($msg,url('cateedit',['cate_id'=>$param['cateid']]));
               }
               if (isset($param['back']) && !empty($param['back'])){
                   $this->success($msg,url('cate'));
               }
           }else{
               $this->error($msg,url('cate'),[],3);
           }
       }

        return View::fetch();
    }

    //todo==========================属性页====================================
    public function prop(){
        //拼接where条件
        list($where,$findId)=GoodsLogic::pinWhere();
        //留值设置
        $data=GoodsLogic::liuData();
        View::assign($data);
        //查询数据
        $plist=\app\admin\model\GoodsProp::getPropList($where,$findId);
        //查询属性值
        $vlist=GoodsPropValue::getVaList();
        View::assign('vlist',$vlist);
       //逗号属性方法
        $plist=GoodsLogic::setDou($plist);


        View::assign("prop",$plist);

        return View::fetch();
    }

     //todo=============================品牌页================================
    public function brand()
    {
        //拼接where
        list($where,$whereOr) = GoodsLogic::bWhere();
        //留置操作
        $data = GoodsLogic::bData();


        //查询数据
        $brand = Brand::getBrandList($where,$whereOr);
        View::assign("brand", $brand);
        View::assign($data);

        return View::fetch();
    }
//todo================================品牌添加====================================
   public function brandAdd(){

        if (Request::isPost()){
            $param=Request::param();
            $data=[
                'brand_name'=>$param['name'],
                'recommended'=>isset($param['recommended'])?$param['recommended']:0,
                'if_show'=>isset($param['if_show'])?$param['if_show']:0,
                'sort_order'=>$param['sort'],
                'tag'=>$param['content']
            ];


            list($res,$msg)=\app\admin\model\Brand::brandAdd($data);
            if ($res){
                $this->success($msg,url('brand'),[],3);
            }else{
                $this->error($msg,url('brand'),[],3);
            }

        }else{
            return View::fetch();
        }

   }
   //todo=============================品牌修改=======================================
   public function brandEdit(){
       if (Request::isGet()){
           $param=Request::param();
           $brands = \app\admin\model\Brand::find($param['brand_id'])->toArray();
           View::assign('brands',$brands);
//           echo "<pre>";
//           print_r($brands);
//           echo "</pre>";

       }else{
           $param=Request::param();
//           echo "<pre>";
//           print_r($param);
//           echo "</pre>";
//           die;
           $data=[
               'brand_id'=>$param['brandid'],
               'brand_name'=>$param['name'],
               'recommended'=>$param['recommended'],
               'if_show'=>$param['if_show'],
               'tag'=>$param['content'],
               'sort_order'=>$param['sort'],
           ];
//           echo "<pre>";
//           print_r($data);
//           echo "</pre>";

           list($res,$msg)=\app\admin\model\Brand::editBrandList($data);
           if ($res){
               //成功
               if (isset($param['come']) && !empty($param['come'])){
                   $this->success($msg,url('brandEdit',['brand_id'=>$param['brandid']]));
               }
               if (isset($param['back']) && !empty($param['back'])){
                   $this->success($msg,url('brand'));
               }

           }else{
               //失败
               $this->error($msg,url('brand'),[],3);
           }

       }

       return View::fetch();
   }
}